package com.android.vgongyi.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.android.vgongyi.R;

/**
 * Point before exiting , make sure user if want to exit or not.
 * @author Arashmen
 * */
public class VersionDialog extends AlertDialog{

	public VersionDialog(final Activity activity,final String website,String versionTip) {
		super(activity);
		this.setOwnerActivity(activity);
		/*Listener for positive button to exit application*/
		DialogInterface.OnClickListener positiveListener = new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
				 Uri uri = Uri.parse(website);  
				 activity.startActivity(new Intent(Intent.ACTION_VIEW,uri));  
				
			}
		};
		
		/*Listener for negative button to get back to application*/
		DialogInterface.OnClickListener negativeListener = new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dismiss();
			}
		};
	
		/*Make sure search button and back button don't work*/
		DialogInterface.OnKeyListener keydownListener = new OnKeyListener() {
			
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if(keyCode == KeyEvent.KEYCODE_SEARCH||keyCode == KeyEvent.KEYCODE_BACK)
					return true;
				return false;
			}
		};
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(activity.getText(R.string.dialog_new_version_to_update).toString());
		if(!TextUtils.isEmpty(versionTip)){
			buffer.append("\n");
			String[] tips = versionTip.split(" ");
			for(String tip:tips){
				buffer.append("\n").append(tip.trim());
			}
			buffer.append("\n");
			
		}
		
		this.setTitle(R.string.dialog_point);
		
		this.setMessage(buffer.toString());
		
		this.setButton(activity.getText(R.string.dialog_sure), positiveListener);
		
		this.setButton2(activity.getText(R.string.dialog_cancel), negativeListener);

		this.setOnKeyListener(keydownListener);
	}
	
	
}
